/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.action;

import com.mx.im.sinocab.auxController.AdminMenuActionAC;
import com.mx.im.sinocab.auxController.LoginActionAC;
import com.mx.im.sinocab.bean.Employ;
import com.mx.im.sinocab.bean.User;
import com.mx.im.sinocab.bean.Usertype;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Javier Aviles Cruz
 */
public class LoginAction extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("/Jsp/body/index.jsp");

        try {
            LoginActionAC lac = new LoginActionAC();

            String user = request.getParameter("user") != null ? request.getParameter("user").toString() : "";
            String password = request.getParameter("password") != null ? request.getParameter("password").toString() : "";

            User userLogin = lac.executeLogIn(new User(0,new Employ(), new Usertype(), user, password, null));
            if (userLogin.isActiveSesion()) {
                AdminMenuActionAC ama = new AdminMenuActionAC();
                request.removeAttribute("listMenuOptions");
                rd = request.getRequestDispatcher("/Jsp/body/JspMainMenu.jsp");
                request.getSession(true).setAttribute("user", userLogin);
                request.getSession().setAttribute("activeSesion", true);
                request.getSession().setAttribute("dateToday", userLogin.getDate());
                request.getSession(true).setAttribute("sesionListMenuOptions", ama.listMenuOptions());
                request.setAttribute("messageType", "alert");
                request.setAttribute("message", "¡Bienvenido " + userLogin.getEmploy().getName() + " " + userLogin.getEmploy().getLastName() + "!");
                rd.forward(request, response);
            } else {
                request.getSession(false).invalidate();
                request.setAttribute("messageType", "error");
                request.setAttribute("message", "¡Por favor, verifique su usuario y contraseña!");
                rd.forward(request, response);
            }
        } catch (NullPointerException npe) {
            System.out.println("E R R O R   N U L L P O I N T E R E X C E P T I O N"
                    + "  (LoginAction.java): -------> \n" + npe.getMessage());
            npe.printStackTrace();
            request.setAttribute("error", "¡Bienvenido!");
        } catch (Exception e) {
            System.out.println("E R R O R   E X C E P T I O N"
                    + "  (LoginAction.java): -------> \n" + e.getMessage());
            e.printStackTrace();
            request.setAttribute("error", "¡Bienvenido!");
        } finally {
            out.close();

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
